/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2017 All Rights Reserved.
 */
package com.fulihui.usercore.core.repository.impl;

import com.fulihui.usercore.biz.conv.GeneralConv;
import com.fulihui.usercore.core.repository.WechatOpenidHelpRepository;
import com.fulihui.usercore.dal.dataobj.WechatOpenidHelpDO;
import com.fulihui.usercore.dal.dataobj.WechatOpenidHelpDOExample;
import com.fulihui.usercore.dal.mapper.WechatOpenidHelpDOMapper;
import com.fulihui.usercore.dto.WechatUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 *  用户授权信息辅助（openid）仓库  实现
 * @author: zhangyingjie
 * @version: v 0.1 2017年10月24日 15:33  zhangyingjie Exp $
 */
@Repository
public class WechatOpenidHelpRepositoryImpl implements WechatOpenidHelpRepository {
    @Autowired
    private WechatOpenidHelpDOMapper wechatOpenidHelpDOMapper;

    @Override
    public void create(WechatUserDTO dto) {
        wechatOpenidHelpDOMapper.insert( GeneralConv.conv(dto, WechatOpenidHelpDO.class));
    }

    @Override
    public List<WechatUserDTO> query(WechatOpenidHelpDOExample example) {
        return GeneralConv.convert2List(wechatOpenidHelpDOMapper.selectByExample(example),WechatUserDTO.class);
    }

    @Override
    public WechatUserDTO querySingle(String openId, String openIdType, String appid) {
        Assert.hasLength(openId);
        Assert.hasLength(openIdType);
        Assert.hasLength(appid);

        WechatOpenidHelpDOExample example = new WechatOpenidHelpDOExample();
        example.createCriteria().andOpenIdEqualTo(openId)
                .andOpenidTypeEqualTo(openIdType)
                .andAppidEqualTo(appid);
        List<WechatOpenidHelpDO> list = wechatOpenidHelpDOMapper.selectByExample(example);
        // 要么空，要么只有1条
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return GeneralConv.conv(list.get(0),WechatUserDTO.class);
    }
}
